const { response } = require('express');
var express = require('express');
var router = express.Router();

// 创建mysql实例
const CreateQuery = require("./mysql")
const create = new CreateQuery()
// 获取数据
router.get('/getList', function (req, response, next) {
    create.init_database()
    create.select('select * from list').then((res) => {
        response.send(res)
    })
    create.destory()
});
// 加入购物车
router.post('/addShopCar',(request,response,next) => {
  console.log(request.body);
  let {id,img,isCheck,shopName,price,oldPrice,label,num,state} = request.body.obj
  console.log(state);
  create.init_database()
  create.select('select * from cardata').then(res => {
        let index = res.findIndex(item => {
            return Number(item.id === id)
        })
        if(index !== -1){
          create.init_database()
          create.select(`update cardata set num='${Number(res[index].num) + Number(num)}' where id=${res[index].id}`)
          response.send('修改num成功')
        }else{
          create.init_database()
          create.select(`insert into cardata(id,img,isCheck,shopName,price,oldPrice,label,num,state)
           values('${id}','${img}','${isCheck}','${shopName}','${price}','${oldPrice}','${label}','${num}','${state}')`)
          response.send('添加成功')
        }
  })
  create.destory()
})


router.get('/order', (req, response) => {
    create.init_database()
    create.select(`select * from carorder`).then((res) => {
        console.log(res);
        response.send(res)
    })
    create.destory()
})
router.get('/Okgoods', (req, response) => {
    console.log(Number(req.query.id));
    create.init_database()
    create.select(`update carorder set state="3" where ifID=${Number(req.query.id)}`)
    create.destory()
})
router.get('/Delgoods', (req, response) => {
    console.log(Number(req.query.id));
    create.init_database()
    create.select(`delete from carorder where ifID=${Number(req.query.id)}`)
    create.destory()
})
router.get('/Okmoney', (req, response) => {
    create.init_database()
    create.select(`update carorder set state="2" where ifID=${Number(req.query.id)}`)
    create.destory()
})

//获取购物车
router.get('/shopList', function (req, response, next) {
  // 连接mysql 
  create.init_database()
  create.select(`select * from cardata`).then((res) => {
    // console.log(res);
    response.send(res)
  })
  create.destory()
});
//加加
router.post('/addBtnList', function (req, response, next) {
  //UPDATE <表名> SET 字段 1=值 1 [,字段 2=值 2… ] [WHERE 子句 ]
  let {
    id,
    num
  } = req.body
  create.init_database()
  create.select(`UPDATE  cardata  SET num='${num}' WHERE  id='${id}'`).then((res) => {
    response.send(res)
  })
  create.destory()
});

router.post('/checkAll', function (req, response, next) {
  //UPDATE <表名> SET 字段 1=值 1 [,字段 2=值 2… ] [WHERE 子句 ]
  let {
    id,
    isCheck
  } = req.body
  create.init_database()
  create.select(`UPDATE  cardata  SET isCheck='${isCheck}' WHERE  id='${id}'`).then((res) => {
    response.send(res)
  })
  create.destory()
});
//减减

router.post('/jianjian', function (req, response, next) {
  console.log(req.body);
  let {
    id,
    num
  } = req.body
  create.init_database()
  create.select(`UPDATE  cardata  SET num='${num}' WHERE  id='${id}'`).then((res) => {
    response.send(res)
  })
  create.destory()
});
router.get('/Homedata', function (req, response) {
  create.init_database()
  create.select(`select * from list`).then(res => {
    console.log(res);
    response.send(res)
  })
  create.destory()
})
//tijiaoAll

router.post('/tijiaoAll', function (req, response) {
  let {
    id,
    img,
    isCheck,
    shopName,
    price,
    label,
    num,
    state
  } = req.body;
  console.log(req.body);
  create.init_database();
  // 获取cardata数据


  create.init_database();
  let sql = `insert into carorder(id,img,isCheck,shopName,price,label,num,state)
       values('${id}','${img}','${isCheck}','${shopName}','${price}','${label}','${num}','${state}')`
  create.select(sql)
  // create.destory()


  create.destory()
  response.send()
})

//付款
router.post('/tjAll', function (req, response, next) {
  console.log(req.body);
  let {
    id,
    state
  } = req.body
  create.init_database()
  create.select(`UPDATE  carorder  SET state='${state}' WHERE  id='${id}'`).then((res) => {
    response.send(res)
  })
  create.destory()
});




//获取分类数据
router.get('/list', function (req, response, next) {
    // response.send('123')
    create.init_database()
    create.select(`select * from xcxdata`).then((res) => {//xcxdata
        // console.log(res, '14');
        response.send(res)
    })
    create.destory()
});

// 添加购物车
router.post('/addShop', function (req, response) {
      let {id,img,isCheck,shopName,price,oldPrice,label,num,state} = req.body.obj
      // console.log(req.body.icon);
      create.init_database();
      // 获取cardata数据
      create.select(`select * from cardata`).then((res) => {
          let index = res.findIndex(item => {
              return item.id === id
          })
          if (index !== -1) {
              create.init_database();
              let Num = Number(res[index].num)
              console.log(Num);
              create.select(`update cardata set num='${Num + num}' where id='${res[index].id}'`)
              // create.destory()
              return
          } else {
              create.init_database();
              let sql = `insert into cardata(id,img,isCheck,shopName,price,oldPrice,label,num,state) 
        values('${id}','${img}','${isCheck}','${shopName}','${price}','${oldPrice}','${label}','${num}','${state}')`
              create.select(sql)
              // create.destory()
              // response.send(res)
          }
      })
      create.destory()
      response.send('添加成功')
  })




//查找数据
router.post('/filter', function (req, response) {
    console.log(req.body);
    let { val } = req.body
    //select * from  shopdata where name='羊肉'
    create.init_database()
    create.select(`select * from xcxdata`).then((res) => {// where name='${val}'
        // console.log(res);
        let index = res.filter((item) => {
            return item.name.indexOf(val) > -1
        })
        response.send(index)
    })
    create.destory()
})



// 详情页接口
//获取购物车列表数据
router.get('/getshopcarList', function (req, response, next) {
  create.init_database();
  create.select('select * from cardata').then(res => {
      response.send(res);
  })
  create.destory();
})
module.exports = router;
